<?php
namespace Api\Controller;
use Think\Controller;
/**
 * Class ApiBasicController
 * @package Api\Controller
 */
class ApiBasicController extends Controller {
    /**
     * 初始化定义
     */
    public $conf = '';//配置全局变量
    public function _initialize(){
        header ("Content-Type:text/html; charset=utf-8" );

    }

    /**
     * 获取配置信息
     */
    public function getConfig(){

    }


    /**
     * 单图片上传图片
     * @author crazy
     * @time 2017-07-03
     */
    public function uploadImg($file,$name){
        $config = array(
            'subName'    =>    array('date','Y-m-d'), //设置文件名
        );
        $upload = new \Think\Upload($config);// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath  =      "$file/"; // 设置附件上传目录    // 上传文件
        $info   =   $upload->upload();
        $savename = $info[$name]['savename'];
        $savepath = $info[$name]['savepath'];
        $a =$savepath.$savename;
        return $a;
    }

    /**
     * 上传视频
     * @author crazy
     * @time 2017-07-03
     */
    public function uploadVideo($file,$name){
        $config = array(
            'subName'    =>    array('date','Ym'), //设置文件名
        );
        $upload = new \Think\Upload($config);// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->savePath  =      "$file/"; // 设置附件上传目录    // 上传文件
        $info   =   $upload->upload();
        $savename = $info[$name]['savename'];
        $savepath = $info[$name]['savepath'];
        $a = $savepath.$savename;
        return $a;
    }
    /**
     * 上传图片
     * @author crazy
     * @time 2017-07-03
     */
    public function uploadImgMore($file){
        $config = array(
            'subName'    =>    array('date','Y-m-d'), //设置文件名
        );
        $upload = new \Think\Upload($config);// 实例化上传类
        $upload->savePath  =      "$file/"; // 设置附件上传目录    // 上传文件
        $info   =   $upload->upload();
        return $info;
    }


    /**
     * 封装用户的分页方法
     * @author crazy
     * @time 2017-07-03
     */
    public function z_page($res,$page,$parameter){
        $list = $res;
        $count = $page; //每页显示的记录数
        $p = new \Think\Page(count($list),$count,$parameter); //实例化分页类 传入总记录数和每页显示的记录数
        $theme = "<ul class='pagination'><li>%TOTAL_ROW%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li></ul>";
        $p->setConfig('theme',$theme);
        $page_info = $p->show();
        $lists = array_slice($list,$p->firstRow,$p->listRows); //在数组中根据条件取出一段值
        $arr['page'] = $page_info;
        $arr['list'] = $lists;
        return $arr;
    }

    

    /**
     *计算两个经纬度之间的距离
     * @author crazy
     * @time 2017-07-03
     *
    */
    public function getDistance($lat1, $lng1, $lat2, $lng2)
    {
        $earthRadius = 6367000; //approximate radius of earth in meters

        /*
          Convert these degrees to radians
          to work with the formula
        */

        $lat1 = ($lat1 * pi() ) / 180;
        $lng1 = ($lng1 * pi() ) / 180;

        $lat2 = ($lat2 * pi() ) / 180;
        $lng2 = ($lng2 * pi() ) / 180;

        /*
          Using the
          Haversine formula

          http://en.wikipedia.org/wiki/Haversine_formula

          calculate the distance
        */

        $calcLongitude = $lng2 - $lng1;
        $calcLatitude = $lat2 - $lat1;
        $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
        $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
        $calculatedDistance = $earthRadius * $stepTwo;

        return round($calculatedDistance);
    }

    /**
     * 获取接口信息
     * @author crazy
     * @time 2017-07-03
     */
    public function httpsRequest($url,$data = ""){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    /**
     * 请求接口返回内容
     * @author crazy
     * @time 2017-07-03
     * @param  string $url [请求的URL地址]
     * @param  string $params [请求的参数]
     * @param  int $ipost [是否采用POST形式]
     * @return  string
     */
    public function juhecurl($url,$params=false,$ispost=0){
        $httpInfo = array();
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
        if( $ispost )
        {
            curl_setopt( $ch , CURLOPT_POST , true );
            curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
            curl_setopt( $ch , CURLOPT_URL , $url );
        }
        else
        {
            if($params){
                curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
            }else{
                curl_setopt( $ch , CURLOPT_URL , $url);
            }
        }
        $response = curl_exec( $ch );
        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
        $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
        curl_close( $ch );
        return $response;
    }

    /**获取城市*/
    public function lngLatCityN($lnt,$lat){
        $url = "http://restapi.amap.com/v3/geocode/regeo?location=$lnt,$lat&key=993b99a1a6e33e15d650000b2573d94d&radius=1000&extensions=all&output=JSON";
        $json = $this->httpsRequest($url);
        $arr = json_decode($json,true);

        /**直辖市不显示市级，所以要使用省找市级*/
        if(empty($arr['regeocode']['addressComponent']['city'])){
            $area_id = M("Areas")->where(array('area_name'=>$arr['regeocode']['addressComponent']['province']))->getField("area_id");
            $city = M("Areas")->where(array('area_id'=>$area_id))->field("area_id,area_name")->find();
            $city['city_name'] = $city['area_name'];
            $city['province_name'] = $arr['regeocode']['addressComponent']['province'];
            $city['area_name'] = $arr['regeocode']['addressComponent']['district'];
            $dis_id = M("Areas")->where(array('parent_id'=>$city['area_id'],'area_name'=>$city['area_name']))->getField("area_id");
            $city['dis_id'] = $dis_id;
            return json_encode($city);
        }else{
            $area_id = M("Areas")->where(array('area_name'=>array('like','%'.$arr['regeocode']['addressComponent']['city'])))->getField("area_id");
            $city = M("Areas")->where(array('area_id'=>$area_id))->field("area_id,area_name")->find();
            $city['city_name'] = $city['area_name'];
            $city['province_name'] = $arr['regeocode']['addressComponent']['province'];
            $city['area_name'] = $arr['regeocode']['addressComponent']['district'];
            $dis_id = M("Areas")->where(array('parent_id'=>$city['area_id'],'area_name'=>$city['area_name']))->getField("area_id");
            $city['dis_id'] = $dis_id;
            return json_encode($city);
        }
    }

}
